package com.czk.gulimall.order.config;

import com.czk.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/5/22 14:32
 */
@Configuration
@Slf4j
public class MyMqConfig {


    @Bean
    public Queue orderDelayQueue() {

        // 设置特殊属性
        Map<String, Object> map = new HashMap<>();
        // 设置死信队列参数
        map.put("x-dead-letter-exchange", "order-event-exchange");
        // 陆游键
        map.put("x-dead-letter-routing-key", "order.release.order");
        // 过期时间
        map.put("x-message-ttl", 60000);
        Queue queue = new Queue("order.delay.queue", true, false, false,map);
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false, null);
        return queue;
    }

    @Bean
    public Exchange orderEventExchange() {
        TopicExchange topicExchange = new TopicExchange("order-event-exchange", true, false, null);
        return topicExchange;
    }

    @Bean
    public Binding orderCreateOrderBingding() {
        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
        return binding;
    }

    @Bean
    public Binding orderReleaseOrderBingding() {
        Binding binding = new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
        return binding;
    }

    /**
     * 订单释放，再次发送解锁库存消息
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingding() {
        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.other.#", null);
        return binding;
    }
}
